#ifndef _SPRITE_H_
#define _SPRITE_H_
#include "utypes.h"
#include "video-graphics.h"
#include <conio.h>
extern int HRES;
extern int VRES;

/** @defgroup Sprite Sprite
* @{
*
* Sprite related functions
*/

/** A Sprite is an "object" that contains all needed information to
* create, animate, and destroy a pixmap.  The functions assume that
* the background is BLACK and they take into account collision with
* other graphical objects or the screen limits. 
*/
typedef struct {
	int x, y;            ///< current sprite position
	int width, height;   ///< sprite dimensions
	int xspeed, yspeed;  ///< sprite current speed in the x and y direction
	int newxspeed, newyspeed; 
	char *map;           ///< the sprite pixmap (use read_xpm())
} Sprite;


#define MAX_SPEED 5    /**< each sprite maximum speed in pixels/frame */

#define MOUSE_COLOR 4	/**< defined mouse cursor color*/

#define NO_COL					0	/**< fig  does not colide*/
#define RIGHT_HIT				2	/**< collision with right block (WHITE)  */
#define LEFT_HIT 				3	/**< collision with left block (WHITE)   */
#define MOUSE_HIT				4	/**< collision with mouse (LIGHTMAGENTA) */
#define COLIDE_TOP			 	10	/**< fig colides with top of the screen*/
#define COLIDE_BOTTOM			11	/**< fig colides with bottom of the screen*/
#define COLIDE_LSIDE			12	/**< fig colides with the left side of the screen*/
#define COLIDE_RSIDE			13	/**< fig colides with the right side of the screen*/
#define COLIDE_FIG				14	/**< fig colides with another figure already on the screen*/

/** Reads a xpm-like sprite defined in "map" (look at pixmap.h for
* examples). Returns the address of the allocated memory where the
* sprite was read. Updates "width" and "heigh" with the sprite
* dimension.  Return NULL on error.
* Assumes that VRES and HRES (the screen vertical and horizontal resolution)
* are externaly defined.
* 
* Usage example, using the defined sprite in pixmap.h:
* <pre>
*   #include "pixmap.h" // defines  pic1, pic2, etc 
*   int wd, hg;
*   char *sprite = read_xpm(pic1, &wd, &hg);
* </pre>
*/
char *read_xpm(char *map[], int *width, int *height);

/** Creates with random speeds (not zero) and pre-defined position
* (within the screen limits), a new sprite with pixmap "pic", in
* memory whose address is "base".
* Returns NULL on invalid pixmap.
*/
Sprite * create_sprite(char *pic[], char *base,int x, int y);

/** Animate the sprite "fig" according to its attributes in memory, 
* whose address is "base".
* The animation detects the screen borders
* and change the speed according; it also detects collision with
* other objects, including the player pad. Collisions with the screen
* border generates a perfect reflection, while collision with other
* objects generates a random perturbation in the object speed. Other
* strategies can be devised: use quasi-elastic collision based on the
* objects "mass" and speed, generate spin effect based on amount of
* tangential speed direction relative to the object center of
* "mass"...  Returns the kind of collision detected, RIGHT_HIT or
* LEFT_HIT, if a collision with the players pad (WHITE colored!) is
* detected.
*/
int animate_sprite(Sprite *fig, char *base, int back, int type);

/**Shows the sprite "fig" in the screen on a defined position*/
void show_sprite_pos(Sprite* fig, int x, int y, char* base) ;

/**Shows the sprite "fig" in the screen*/
void show_sprite(Sprite* fig, char* base);

/**Trys a new direction for the sprite*/
void tryNewDirection(Sprite *fig, char *base, int back, int type);

/**The "fig" sprite is not showed and where it was, we see background*/
void erase_sprite(Sprite* fig, int back, char* base) ;

/** The "fig" sprite is not showed and where it was, we see background and
* The "fig" sprite is erased from memory whose address is "base"
* and used resources released.
*/
void destroy_sprite(Sprite *fig, char *base, int back);

/** The "fig" sprite is erased from memory whose address is "base"
* and used resources released.
*/
void free_sprite(Sprite *fig);

/** Move in memory whose address is 'base', the 'fig' cursor, a
* standard sprite, from its current position to a new position
* 'xstep' and 'ystep' away.  
* The cursor is erased from its present position in xor-mode and draw
* in the new position also oin xor-mode.
* The first time this function is called it only draws the cursor at the
* current position.
*/ 
void move_cursor(Sprite *fig, int xstep, int ystep, char *base, int back);



/** @} end of sprite */

#endif
